package com.zhixin.zhixinwaimai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhixin.zhixinwaimai.common.R;
import com.zhixin.zhixinwaimai.dto.SetmealDto;
import com.zhixin.zhixinwaimai.entity.Category;
import com.zhixin.zhixinwaimai.entity.Dish;
import com.zhixin.zhixinwaimai.entity.Setmeal;
import com.zhixin.zhixinwaimai.entity.SetmealDish;
import com.zhixin.zhixinwaimai.mapper.CategoryMapper;
import com.zhixin.zhixinwaimai.mapper.SetmealMapper;
import com.zhixin.zhixinwaimai.service.CategoryService;
import com.zhixin.zhixinwaimai.service.SetmealDishService;
import com.zhixin.zhixinwaimai.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    SetmealDishService setmealDishService;
    @Override
    @Transactional
    public void saveWithsetmealDishs(SetmealDto setmealDto) {
        //先将数据添加到套餐表
        this.save(setmealDto);
        //获得提交的套餐id
        Long id = setmealDto.getId();
        //提交的菜品集合
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //遍历菜品集合
        for (SetmealDish setmealDishe:setmealDishes) {
            setmealDishe.setSetmealId(id);
            setmealDishService.save(setmealDishe);
        }
    }
    @Override
    @Transactional
    public boolean deleteWithsetmealDishs(List<Long> ids) {

        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId,ids);
        lqw.eq(Setmeal::getStatus,1);
        int count = this.count(lqw);
        if (count>0){
            return false;
        }
        LambdaQueryWrapper<Setmeal> lqw1 = new LambdaQueryWrapper<>();
        lqw1.in(Setmeal::getId,ids);
        lqw1.eq(Setmeal::getStatus,0);
        this.remove(lqw1);
        LambdaQueryWrapper<SetmealDish> lqw2 = new LambdaQueryWrapper<>();
        lqw2.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(lqw2);
        return true;
    }
    @Override
    @Transactional
    public SetmealDto getByIdWithDish(Long id) {
        //查询套餐基本信息
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);

        //查询套餐菜品信息
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> list = setmealDishService.list(queryWrapper);

        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }
    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        //更新setmeal表基本信息
        this.updateById(setmealDto);
        //更新setmeal_dish表信息delete操作
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(queryWrapper);
        //更新setmeal_dish表信息insert操作
        List<SetmealDish> SetmealDishes = setmealDto.getSetmealDishes();
        SetmealDishes = SetmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(SetmealDishes);
    }



}
